<title>操作日志</title>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>系统监控</cite></a>
        <a><cite>操作日志</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body layui-form">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" name="keywords" placeholder="请输入关键词" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" lay-submit lay-filter="LAY-maintain-database-search"><i class="layui-icon layui-icon-search"></i></button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-maintain-database-manage" lay-filter="LAY-maintain-database-manage"></table>
            <script type="text/html" id="LAY-maintain-database-toolbar">
                <div class="layui-btn-container">
                    {{# if (AppGeneral.userCan('maintain.database.optimize')){ }}
                    <btton class="layui-btn layui-btn-sm" lay-event="optimize">优化</btton>
                    {{# } }}
                    {{# if (AppGeneral.userCan('maintain.database.repair')){ }}
                    <btton class="layui-btn layui-btn-sm layui-btn-normal" lay-event="repair">修复</btton>
                    {{# } }}
                </div>
            </script>
        </div>
    </div>
</div>
<script>
    layui.use(['index', 'baseMethods'], function (){
        const {form, layer, table, baseMethods} = layui, tableId = 'LAY-maintain-database-manage';
        const tableIns = baseMethods.table({
            elem: '#'+tableId,
            url: '/maintain/database/list',
            page: false,
            toolbar: '#LAY-maintain-database-toolbar',
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'name', title: '表名称'},
                {field: 'engine', title: '表引擎', width: 90},
                {field: 'rows', title: '总行数',sort: true, width: 110},
                {field: 'data_free', title: '碎片大小', width: 110},
                {field: 'data_length', title: '数据大小', width: 110},
                {field: 'index_length', title: '索引大小', width: 110},
                {field: 'collation', title: '字符集', width: 180},
                {field: 'comment', title: '表注释'},
                {field: 'create_time', title: '创建时间', width: 180},
                {fixed: 'right', width: 180, align: 'center',
                    templet: function (){
                        let html = '<div class="layui-btn-group">\n <btton class="layui-btn layui-btn-sm" lay-event="show">查看</btton>\n';
                        if (AppGeneral.userCan('maintain.database.clear')){
                            html += '<btton class="layui-btn layui-btn-sm layui-btn-danger" lay-event="clear">清空</btton>\n';
                        }
                        return html + '</div>\n';
                    }
                }
            ]]
        });
        let tableTools = {
            optimize: function (obj){
                const hasCheck = table.checkStatus(obj.config.id), hasCheckData = hasCheck.data;
                if (hasCheckData.length > 0){
                    baseMethods.toolOpera({
                        title: `确认优化选中的 ${hasCheckData.length} 项吗？`,
                        request: {
                            url: '/maintain/database/optimize',
                            type: "post",
                            data: {tables: hasCheckData.map(item => item.name)},
                        },
                    })
                    return true;
                }
                layer.msg('请选择操作项', {icon:5});
            },
            repair: function (obj){
                const hasCheck = table.checkStatus(obj.config.id), hasCheckData = hasCheck.data;
                if (hasCheckData.length > 0){
                    baseMethods.toolOpera({
                        title: `确认修复选中的 ${hasCheckData.length} 项吗？`,
                        request: {
                            url: '/maintain/database/optimize',
                            type: "post",
                            data: {tables: hasCheckData.map(item => item.name)},
                        },
                    })
                    return true;
                }
                layer.msg('请选择操作项', {icon:5});
            },
            show: function (obj){
                const data = obj.data;
                baseMethods.adminLayer([
                    {title: '查看', area: ['650px', '580px'], id: 'LAY-popup-database-show', btn: false},
                    {tpl: "maintain/database/show", mode: 'show', data},
                ])
            },
            clear: function (obj){
                const data = obj.data;
                baseMethods.toolOpera({
                    title: '确认清空吗？',
                    request: {
                        url: '/maintain/database/clear',
                        type: "post",
                        data: {table: data.name},
                    },
                })
            },
        };
        table.on('toolbar('+tableId+')', function(obj){
            tableTools[obj.event] ? tableTools[obj.event].call(this, obj) : '';
        });
        table.on('tool('+tableId+')', function (obj){
            tableTools[obj.event] ? tableTools[obj.event].call(this, obj) : '';
        });
        form.on('submit(LAY-maintain-database-search)', function(data){
            tableIns.reloadData({
                where: data.field,
                page: {curr:1}
            })
        });
    });
</script>
